export default class ItemLogic {
    constructor(){

    }
    private static _instance :ItemLogic;
    /**
     * 实例化对象
     */
    
    public  static get instance() : ItemLogic {
        if(!this._instance)
        {
            this._instance = new ItemLogic();
        }
        return this._instance;
    }
    private _itemList:Array<object> = [
        {"id":101,"num":2},
        {"id":102,"num":2},
        {"id":103,"num":2},
        {"id":104,"num":2},
        {"id":105,"num":2},
        {"id":106,"num":2},
        {"id":107,"num":2},
        {"id":108,"num":2},
        {"id":109,"num":2},
        {"id":110,"num":2},
    ];
    public get itemList() : Array<object> {
        return this._itemList;
    }
    private addItem(itemId:number,num:number) {
        for (const key in this._itemList) {
            if (this._itemList.hasOwnProperty(key)) {
                const item:object = this._itemList[key];
                if(itemId==item["id"])
                {
                    item["num"] = item["num"]+num
                    if(item["num"]<=0)
                    {
                        this._itemList.splice(Number(key),1)
                    }
                    return
                }
            }
        }
        if (num>0)
        {
            this._itemList.push({"id":itemId,"num":num})
        }
    }
    private useItemEffect(itemId:number,num:number)
    {

    }
    public useItem(itemId:number,num:number) {
        this.addItem(itemId,-num)
        this.useItemEffect(itemId,num)
        cc.sys.localStorage.setItem("itemList",this._itemList)
        return this._itemList;
    }
    public lostItem(itemId:number,num:number) {
        this.addItem(itemId,-num)
        cc.sys.localStorage.setItem("itemList",this._itemList)
        return this._itemList;
    }
}
